home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 176-200 / scopedisk180 / arexxtutorial / listenv / listenv.tutor < prev    next >
Text File  |  1995-03-19  |  7KB  |  243 lines

  1. /* ListEnv.rexx */
  2.  
  3. /* List Amiga "ENV:" file style environment variables.
  4.  *
  5.  * Note: For programming, I like tabs set at 4, not 8, so this may
  6.  * look funny at 8!
  7.  */
  8.  
  9. /* Written by  Don Meyer  (Stormgate Software).
  10.  * BIX:        donmeyer
  11.  * GEnie:    D.MEYER
  12.  * Plink:    STORMGATE
  13.  *
  14.  * This program released into the Public Domain.
  15.  */
  16.  
  17. /* Modifications by Jeremy Farrance (ETC Software).
  18.  * Extensive documentation too!
  19.  * Plink:    JEREMY
  20.  * 
  21.  * This program remains in the Public Domain.
  22.  * 
  23.  * Bananas
  24.  */
  25.  
  26. /*        Ver.      Date        Who                    Notes
  27.  *        ----    ---------    ---        -----------------------------
  28.  *        0.0        12-Feb-89    DTM        Creation
  29.  *        0.1        19-Mar-89    DTM        Accept a command line env var name.
  30.  *
  31.  *        0.2        17-Apr-89    JRF        General improvements, fixed ENV names
  32.  *                                    with ' ' spaces, added '?' Usage:,
  33.  *                                    added opensupport(), now checks if
  34.  *                                    ENV: assignment exists and added
  35.  *                                    a files listed count, others...
  36.  */
  37.  
  38. /* Let ARexx know we want user CTRL-C's trapped... see BREAK_C: below */
  39.  
  40. signal on BREAK_C
  41.  
  42.  
  43. /* Opens rexxsupport.library if its not (~) already open */
  44.  
  45. if ~show('L',"rexxsupport.library") then do
  46.    if addlib('rexxsupport.library',0,-30,0) then
  47.       say 'added rexxsupport.library'
  48.    else do;
  49.       say 'support library not available'
  50.       exit 10
  51.    end
  52. end
  53.  
  54.  
  55. /* Uses the 'Assign' option of showlist() to verify that ENV: is there */
  56. /* See the 1.06 update.doc for more info */
  57.  
  58. if ~showlist('A','ENV') then do
  59.    say 'ENV: not assigned.'
  60.    exit 10
  61. end
  62.  
  63.  
  64. /* Get the (if any) command line arguments */
  65.  
  66. arg filename
  67.  
  68.  
  69. /* if the command line argument was a '?' then we display usage */
  70.  
  71. if filename == "?" then do
  72.    say 'Usage: ListEnv [Environment variable name]'
  73.    exit 0
  74. end
  75.  
  76.  
  77. /* I added this in at the last minute.  See if you can figure out what */
  78. /* it does, if not leave me EMail on PLink and I'll explain it.   :-)  */
  79.  
  80. if length(filename) = 1 & datatype(filename,'U') then pattern = filename
  81.  
  82.  
  83. /* If the command line argument was not "" then we take the argument as  */
  84. /* a single file name a perform similar to the AmigaDOG 'getenv' command */
  85.  
  86. if filename ~= "" & pattern = 'PATTERN' then do
  87.    call got_one(filename)
  88.    exit 0
  89. end
  90.  
  91.  
  92. /* If we get to here, the user did not specify a file name on the
  93.  * command line, so show the contents of all environment variables. */
  94.  
  95.  
  96. /* Get a list of the files in the ENV: directory using ":" as separator */
  97. /* See the 1.06 update.doc for this enhancement to showdir() */
  98.  
  99. files = showdir( "ENV:", "File", ":" )
  100.  
  101.  
  102. /* initialize count to '0' otherwise when we use it below in the
  103.  * expression  'count = count + ...' we get an error because 'count's
  104.  * uninitialized is 'count = "COUNT"' just like all ARexx variable
  105.  * the error being that "COUNT" is not a number and cant be added (+)
  106.  * to anything.  Try commenting out this line to see what happens     */
  107.  
  108. count = 0
  109.  
  110.  
  111. /* Main 'do' loop - extracts filenames as separated by ':'.
  112.  * See the ARexx manual page(s) 79-80 for more info on the technique
  113.  * used below to 'parse' the result of the above showdir() command   */
  114.  
  115. do forever
  116.    parse upper var files name ':' files
  117.  
  118. /* This breaks us out of the main 'do' loop when all the names have
  119.  * been 'parse'd out of files                                        */
  120.  
  121.    if name == '' then leave
  122.  
  123.  
  124. /* Ignore any icon files which may be present, by using the 'iterate'
  125.  * command we effectively transfer control to the 'end' statement below */
  126.  
  127.    if right(name, 5) == ".INFO" then iterate
  128.  
  129.  
  130. /* This is the rest of the last minute addition that you should be
  131.  * figuring out...                                                    */
  132.  
  133.    if pattern ~= 'PATTERN' & left(name,1) ~= pattern then iterate
  134.  
  135.  
  136. /* By combining the actual function call with the loops counter we can
  137.  * act a little bit like 'C' here...                                   */
  138.  
  139.    count = count + got_one(name)  /* This takes us to the funtion below */
  140.  
  141. end        /* This is the 'end' of the Main Loop AND where the 'iterate's
  142.          * sort of 'end'-up.                                           */
  143.  
  144.  
  145. /* Now just see how many files we displayed for the user */
  146. /* If we didn't display any, then we 'say' so */
  147.  
  148. if count > 0 then say count' files'
  149. else say 'No environment variables found'
  150.  
  151.  
  152. /* And this statement tells ARexx we are done here and can exit the program */
  153.  
  154. exit 0
  155.  
  156.  
  157. /*------------------------------------------------------------------*/
  158. /* Handle opening a file and printing out it's (partial) contents.  */
  159.  
  160. got_one:    procedure
  161.  
  162.  
  163. /* get the argument that was sent to this function in the variable 'name' */
  164.  
  165. arg name
  166.  
  167.  
  168. /* If we cant open (find) the requested file, we tell the user about it
  169.  * and return '0' so that the 'count'er above does not get incremented */
  170.  
  171. if ~open( src, "ENV:" || name, "Read" ) then do
  172.    say "** Unable to access 'ENV:" || name || "' **"
  173.    return(0)
  174. end
  175.  
  176.  
  177. /* 'src' is like an AmigaDOS file handle now, and we can use it to read */
  178.  
  179. value = readln( src )
  180.  
  181.  
  182. /* We use 'equals' for 2 reasons, 1) because it will be fully evaluated
  183.  * and assigned a value below and 2) this way we don't have ARexx using
  184.  * up memory on variables that are a waste                              */
  185.  
  186. equals = readch( src, 1 )    /* Get the LF, if there is one throw it away */
  187.  
  188.  
  189. /* if we haven't reached the end of the file, then we append a visual
  190.  * marker at the end of the value so the user knows there was more than
  191.  * one line in the environment variable                                 */
  192.  
  193. if ~eof( src ) then value = value || '1B'x || "[33m ...<more>" || '1B'x || "[31m"
  194.  
  195.  
  196. /* if length of 'value' is less than 50 then everything will look nice */
  197.  
  198. if length(value) < 50 then equals = " = "
  199.  
  200.  
  201. /* if not we need to adjust what will be displayed.  We do this by adding
  202.  * a LineFeed ('0A'x) to the '=' sign.  This will force the 'value' to
  203.  * print on a new line below.                                            */
  204.  
  205. else do
  206.    equals = '1B'x || "[32m =" || '1B'x || "[31m" || '0A'x || "   "
  207.  
  208.  
  209. /* AND if the length of the 'value' is still too long for the display
  210.  * then we chop it and add '...' to the end to let the user know      */
  211.  
  212.    if length(value) > 69 then
  213.       value = space(left(value,69),0) || '1B'x || "[32m..." || '1B'x || "[31m"
  214.  
  215. end    /* 'end' for the above 'else do' */
  216.  
  217.  
  218. /* Now we report our findings to the user... */
  219.  
  220. say left(name,20) || equals || '"' || value || '"'
  221.  
  222.  
  223. /* Clean up by closing the file we opened */
  224.  
  225. call close(src)
  226.  
  227.  
  228. /* And 'return' a '1' to the calling function because its keeping 'count' */
  229.  
  230. return(1)
  231.  
  232.  
  233. /*-----------------------------------------------------------------------*
  234.  * User pressed control-C.
  235.  * We set this up by the 'signal on BREAK_C' at the start of the program */
  236.  
  237. break_c:
  238.  
  239. say "*** Control-C recieved.  Stopped by user. ***"
  240. exit 5
  241.  
  242. /*------------------------------------------------------------------*/
  243.